package com.ruiyian.project.common.service.impl;

import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.utils.file.ZipUtil;
import com.ruiyian.common.utils.http.HttpUtils;
import com.ruiyian.project.common.service.ApiService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2022/7/26
 * Desc: 仿真数据对接
 */
@Slf4j
@Service
public class ApiServiceImpl implements ApiService {
    @Value("${simulation.httpUrl}")
    private String httpUrl;

    @Value("${api.profile}")
    private String outPath;



    /**
     * 源数据推送
     * @param fileName
     * @param file
     * @return
     */
    @Override
    public AjaxResult toSourceData(String fileName, List<File> file) {
        JSONObject json = new JSONObject();
        try {
            FileOutputStream fos = new FileOutputStream(outPath+"\\zip\\"+fileName+".zip");
            ZipUtil.toZip(file,fos);
            json.put("DataFolder", fileName);
            json.put("File", new File(outPath+"\\zip\\"+fileName+".zip"));
            String result = HttpUtils.sendPost(httpUrl + "/in/RawData", json.toString());
            if (StringUtils.isNotBlank(result)) {
                JSONObject jsonObject = JSONObject.fromObject(result);
                if (jsonObject.getBoolean("success")) {
                    return AjaxResult.success();
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.error();
    }

    /**
     * 原始数据到达 消息推送
     * @return
     */
    @Override
    public AjaxResult toSourceDataMsg() {
        JSONObject json = new JSONObject();
        try {
            json.put("MSG_TYPE", "raw_data");
            String result = HttpUtils.sendPost(httpUrl + "/in/Event", json.toString());
            if (StringUtils.isNotBlank(result)) {
                JSONObject jsonObject = JSONObject.fromObject(result);
                if (jsonObject.getBoolean("success")) {
                    return AjaxResult.success();
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.error();
    }
    /**
     * 查询数据
     * * @param dataType  flow.dat
     * @return
     */
    @Override
    public AjaxResult queryData(String dataType) {
        JSONObject json = new JSONObject();
        try {
            json.put("DATA_TYPE", dataType);
            String result = HttpUtils.sendGet(httpUrl + "/qry/QryData", json.toString());
            if (StringUtils.isNotBlank(result)) {
                JSONObject jsonObject = JSONObject.fromObject(result);
                if (jsonObject.getBoolean("success")) {
                    JSONObject body = jsonObject.getJSONObject("body");
                    return AjaxResult.success(body.getJSONArray("data"));
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.error();
    }
}
